package ui.prototypes;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class CashFlowProvider extends ContentProvider {

    private CashFlowDatabaseHelper mDatabaseHelper;

    private UriMatcher mURIMatcher;

    private static final int URI_CASH_TABLE = 1;

    private static final int URI_CASH_TABLE_ITEM = 2;

    private static final int URI_SUMMARY = 3;

    @Override
    public boolean onCreate() {
        mDatabaseHelper = new CashFlowDatabaseHelper(getContext());

        mURIMatcher = new UriMatcher(0);
        mURIMatcher.addURI(CashFlowContract.AUTHORITY, CashFlowContract.CashFlowTable.TABLE_NAME, URI_CASH_TABLE);
        mURIMatcher.addURI(CashFlowContract.AUTHORITY, CashFlowContract.CashFlowTable.TABLE_NAME+"/#", URI_CASH_TABLE_ITEM);
        mURIMatcher.addURI(CashFlowContract.AUTHORITY, CashFlowContract.CashFlowTable.SUMMARY_NAME, URI_SUMMARY);

        return true;
    }


    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {

        Uri _uri = null;

        SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();

        long id = db.insert(CashFlowContract.CashFlowTable.TABLE_NAME, null, values);

        if(id != -1) {
            _uri = Uri.withAppendedPath(uri, String.format("%d", id));
            getContext().getContentResolver().notifyChange(_uri, null);
        }

        return _uri;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String order) {

        Cursor cursor = null;
        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();

        switch(mURIMatcher.match(uri)) {
        case URI_CASH_TABLE:
            cursor = db.query(CashFlowContract.CashFlowTable.TABLE_NAME, projection, selection, selectionArgs, null, null, order);
            break;
        case URI_CASH_TABLE_ITEM:
            selection = ((selection != null)? selection + " " : "") + CashFlowContract.CashFlowTable._COUNT + " = " + uri.getLastPathSegment();

            cursor = db.query(CashFlowContract.CashFlowTable.TABLE_NAME, projection, selection, selectionArgs, null, null, order);
            break;
        case URI_SUMMARY:
        }

        return cursor;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

        int count = 0;

        SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();

        switch(mURIMatcher.match(uri)) {
        case URI_CASH_TABLE:
            count = db.update(CashFlowContract.CashFlowTable.TABLE_NAME, values, selection, selectionArgs);
            break;
        case URI_CASH_TABLE_ITEM:
            selection = ((selection != null)? selection + " " : "") + CashFlowContract.CashFlowTable._ID + " = " + uri.getLastPathSegment();
            count = db.update(CashFlowContract.CashFlowTable.TABLE_NAME, values, selection, selectionArgs);
            break;
        }

        getContext().getContentResolver().notifyChange(uri, null);

        return count;

    }

}
